﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Avocado.Seckill.IQIYI
{
    public class IqiyiFun
    {
        /// <summary>
        /// 获取时间戳
        /// </summary>
        /// <returns></returns>
        public static long GetUnixTime()
        {
            DateTime dt = DateTime.Now;
            DateTime dateStart = new DateTime(1970, 1, 1, 8, 0, 0);
            return Convert.ToInt64((dt - dateStart).TotalMilliseconds);
        }


        public static string Os()
        {
            long t = GetUnixTime();
            string template = "xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx";
            Random random = new Random();
            string result = "";
            foreach (var item in template)
            {
                int n = (int)((t + 16 * random.NextDouble()) % 16);
                t = t / 16;
                result += item.ToString().Replace("x", n.ToString("x")).Replace("y", "8");
                ;
            }
            return result;
        }


        /// <summary>
        /// 对象转换为字典
        /// </summary>
        /// <param name="obj">待转化的对象</param>
        /// <returns></returns>
        public static Dictionary<string, string> ObjectToMap(object obj)
        {
            Dictionary<string, string> map = new Dictionary<string, string>(); //

            Type t = obj.GetType(); // 获取对象对应的类， 对应的类型string

            PropertyInfo[] pi = t.GetProperties(BindingFlags.Public | BindingFlags.Instance); // 获取当前type公共属性io

            foreach (PropertyInfo p in pi)
            {
                MethodInfo m = p.GetGetMethod();
                if (m != null && m.IsPublic)
                {
                    // 进行判NULL处理
                    if (m.Invoke(obj, new object[] { }) != null)
                    {
                        map.Add(p.Name, m.Invoke(obj, new object[] { }).ToString()); // 向字典添加元素
                    }
                }
            }
            return map;
        }



        /// <summary>
        /// 对象转换为字典
        /// </summary>
        /// <param name="obj">待转化的对象</param>
        /// <returns></returns>
        public static string DicToGetString(Dictionary<string, string> dic)
        {
            List<string> list = new List<string>();
            foreach (var item in dic)
            {
                list.Add($"{item.Key}={item.Value}");
            }
            return string.Join("&", list.ToArray());
        }
    }
}
